//
//  MeViewController.m
//  fitness-iOS
//
//  Created by 卢仕彤 on 2017/11/7.
//  Copyright © 2017年 卢仕彤. All rights reserved.
//

#import "MeViewController.h"
#import "LoginViewController.h"
#import "SetViewController.h"
#import "MeInfoViewMode.h"
#import "FFNetworkHelper.h"
#import "FFCache.h"
#import "BodyFileViewController.h"
#import "RegisteViewController.h"
#import "MeinfoMode.h"
#import "LoginViewController.h"
#import "BaseInfoViewController.h"
#import "FFDataMode.h"

#define kMeHeadInfoCell @"MeHeadInfoCell"
#define kMeCenterCell @"MeCenterCell"
#define kMeDynamicCell @"MeDynamicCell"

@interface MeViewController ()<MeinfoClickDelegate> {
    NSMutableArray *_sections;
    MeinfoMode *_meModel;
}

@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.tableView.canReload = YES;
    [self registCell];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"我";
    [self getUserInfo];
    [self setLeftNavigationRetunItem];
    _meModel = [[MeinfoMode alloc] init];
}

- (void)initWeb {
    NSString *requestUrl = @"login/doLogin";
    
    NSDictionary *parmsDic = @{@"phone":@"15167100152",
                               @"password":@"123456",
                               @"code":@(123456)
                               };
    [FFNetworkHelper openLog];
    [FFNetworkHelper POST:requestUrl parameters:parmsDic success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            [FFCache setToken:responseObject[@"data"][@"token"]];
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    LoginViewController *login  = [[LoginViewController alloc] init];
    [self.navigationController pushViewController:login animated:YES];
}

#pragma -mark 注册cell

- (void)registCell{
    [self.tableView registerClass:[MeHeadInfoCell class] forCellReuseIdentifier:kMeHeadInfoCell];
    [self.tableView registerClass:[MeCenterCell class] forCellReuseIdentifier:kMeCenterCell];
    [self.tableView registerClass:[MeDynamicCell class] forCellReuseIdentifier:kMeDynamicCell];
}

#pragma -mark 页面逻辑

- (NSArray *)customSections {
    if (_sections == nil) {
        _sections = [NSMutableArray array];
        // 头部的cell主要包括上面的webView
        MCUTableCustomSection *sec = [MCUTableCustomSection createWithTarget:self];
        sec = [MCUTableCustomSection createWithTarget:self];
        sec.cellForRowAtIndexSelector = @selector(cellViewForMeheadCell:);
        sec.heightForCellSelector = @selector(heightCellForMeheadCell:);
        sec.cellSelectedSelector = @selector(selectedForMeheadCell:);
        [_sections addObject:sec];
        
        //中间的cell
        sec = [MCUTableCustomSection createWithTarget:self];
        sec.numberOfRowsSelector = @selector(numberOfRowForMeCenterCell:);
        sec.cellForRowAtIndexSelector = @selector(cellViewForMeCenterCell:);
        sec.heightForCellSelector = @selector(heightCellForMeCenterCell:);
        sec.cellSelectedSelector = @selector(selectCellForMeCenterCell:);
        [_sections addObject:sec];

        //底部的cell
        sec = [MCUTableCustomSection createWithTarget:self];
        sec.numberOfRowsSelector = @selector(numberOfRowForMeBottomCell:);
        sec.cellForRowAtIndexSelector = @selector(cellViewForMeBottomCell:);
        sec.heightForCellSelector = @selector(heightCellForMeBottomCell:);
        sec.cellSelectedSelector = @selector(selectCellForMeBottomCell:);
        [_sections addObject:self.splitSection];
        [_sections addObject:sec];
    }
    return _sections;
}

#pragma -mark 相关代理方法

- (NSNumber *)heightCellForMeheadCell:(NSIndexPath *)indexPath {
    return  @125;
}

- (void)selectedForMeheadCell:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    BaseInfoViewController *baseVC  = [[BaseInfoViewController alloc] init];
    baseVC.mode = _meModel;
    [self.navigationController pushViewController:baseVC animated:YES];
}

- (UITableViewCell *)cellViewForMeheadCell:(NSIndexPath *)indexPath {
    MeHeadInfoCell *cell = [[MeHeadInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMeHeadInfoCell];
    [cell setMode:_meModel];
    return cell;
}

- (NSNumber *)numberOfRowForMeBottomCell:(NSIndexPath *)indexPath {
    return  @1;
}

- (NSNumber *)heightCellForMeCenterCell:(NSIndexPath *)indexPath {
    return  @50;
}

- (void)selectCellForMeCenterCell:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableViewCell *)cellViewForMeCenterCell:(NSIndexPath *)indexPath {
    MeCenterCell *cell = [[MeCenterCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMeCenterCell];
    return cell;
}

- (NSNumber *)numberOfRowForMeCenterCell:(NSIndexPath *)indexPath {
    return  @1;
}

- (NSNumber *)heightCellForMeBottomCell:(NSIndexPath *)indexPath {
    return  @(Main_Screen_Width/3);
}

- (void)selectCellForMeBottomCell:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableViewCell *)cellViewForMeBottomCell:(NSIndexPath *)indexPath {
    MeDynamicCell *cell = [[MeDynamicCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMeDynamicCell];
    cell.delegate = self;
    return cell;
}


- (void)setLeftNavigationRetunItem {
    UIButton *rightNavigationButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightNavigationButton setImage:[UIImage imageNamed:@"me_set_button_pic"]
                           forState:UIControlStateNormal];
    rightNavigationButton.frame = CGRectMake(0, 0, 16, 16);
    [rightNavigationButton addTarget:self action:@selector(pushSettingViewController) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightNavigationButton];
    
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self addRightNavigationButtonWithImge:@"me_messgae_button_pic"];
    [self.rightNavigationButton addTarget:self action:@selector(clickMessageButtonClick) forControlEvents:UIControlEventTouchUpInside];
}

-(void)clickMessageButtonClick {
    
}

- (void)pushSettingViewController {
    SetViewController *vc =[[SetViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
//    LoginViewController *setVC = [[LoginViewController alloc]init];
//    [self.navigationController presentViewController:setVC animated:YES completion:nil];
}

- (void)getUserInfo {
    [[FFDataMode shareCenter] get_myInfoModelAPI:^(BOOL finished) {
        _meModel = [FFDataMode shareCenter].myInfoModel;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    }];
}

#pragma -mark ------------------button的点击事件----------------------

- (void)clickMeBaseActionButton:(UIButton *)clickButton {
    NSLog(@"%ld",clickButton.tag);
    if (clickButton.tag == 101) {
        BodyFileViewController *bodyVC = [[BodyFileViewController alloc] init];
        [self.navigationController pushViewController:bodyVC animated:YES];
    }
}
@end
